/**
 * 
 */
package com.base.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.alibaba.fastjson.JSON;
import com.base.model.Command;
import com.base.model.LoginUser;
import com.base.model.SimpleMessage;

/**
 * @author leiruiqi
 *
 */
public class HelloClient2 {
	public  String host = "10.161.165.117";
    public  int port = 7878;
    public  Channel ch;
    public  EventLoopGroup group;
    public LoginUser currentUser; 
    
    public void sendMessage(String msg){
    	try {
    		/*SimpleMessage simpleMessage = new SimpleMessage();
			simpleMessage.setFromUser(currentUser);
			simpleMessage.setTargetUsesrId(1001L);
			simpleMessage.setMsg(msg);*/
    		Command command = new Command();
    		command.setAction("MSG");
    		
    		command.setFromUserId(String.valueOf(currentUser.getId()));
    		
    		SimpleMessage simpleMessage = new SimpleMessage();
    		simpleMessage.setFromUser(this.currentUser);
    		simpleMessage.setTargetUsesrId(1002L);
    		simpleMessage.setMsg(msg);
    		
    		String content = JSON.toJSONString(simpleMessage);
    		command.setContent(content);

    		String jsonStr = JSON.toJSONString(command);
			ch.writeAndFlush(jsonStr);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public void startTestConnect() {
    	group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
            .channel(NioSocketChannel.class)
            .handler(new HelloClientInitializer());

            // 连接服务端
            ch = b.connect(host, port).sync().channel();
            
            LoginUser user = new LoginUser();
			user.setId(1002L);
			user.setName("Mike");
			ch.write(user);
			currentUser = user;
         
			Command command = new Command();
    		command.setAction("LGI");
    		
    		command.setFromUserId(String.valueOf(currentUser.getId()));
    		
    		String content = JSON.toJSONString(this.currentUser);
    		command.setContent(content);

    		String jsonStr = JSON.toJSONString(command);
    		
            // 控制台输入
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for (;;) {
                String line = in.readLine();
                if (line == null) {
                    continue;
                }
                
                 //* 向服务端发送在控制台输入的文本 并用"\r\n"结尾
                 //* 之所以用\r\n结尾 是因为我们在handler中添加了 DelimiterBasedFrameDecoder 帧解码。
                 //* 这个解码器是一个根据\n符号位分隔符的解码器。所以每条消息的最后必须加上\n否则无法识别和解码
                // * 
               this.sendMessage(line);
            }
        } catch(Exception e){
        	e.printStackTrace();
        }finally {
           System.out.println("finally");
        }
    }
    
    public void shutdownGracefully(){
    	group.shutdownGracefully();
    }
    
    public LoginUser getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(LoginUser currentUser) {
		this.currentUser = currentUser;
	}

	/**
     * @param args
     * @throws InterruptedException 
     * @throws IOException 
     */
    public static void main(String[] args) throws InterruptedException, IOException {
    	HelloClient2 helloClient = new HelloClient2();
    	
    	LoginUser user = new LoginUser();
    	user.setId(1001L);
    	user.setName("1001ricky");
    	
    	helloClient.setCurrentUser(user);
    	
    	helloClient.startTestConnect();
    	
    }
}
